<template>
  <div class="bottom-view">
    <div class="view">
      <el-card shadow="hover">
        <template v-slot:header>
          <div class="title">关键词搜索</div>
        </template>
        <div class="chart-wrapper">
          <div class="chart-inner">
            <div class="chart">
              <div class="chart-title">搜索用户数</div>
              <div class="chart-data">97,675</div>
              <v-chart :option="searchUserOption" />
            </div>
            <div class="chart">
              <div class="chart-title">搜索量</div>
              <div class="chart-data">197,675</div>
              <v-chart :option="searchNumberOption" />
            </div>
          </div>
          <div class="table-wrapper">
            <el-table
              :data="tableData"
              :header-cell-style="{ padding: '12px' }"
              :cell-style="{ padding: '12px' }"
            >
              <el-table-column prop="rank" label="排名" width="180" />
              <el-table-column class="column" prop="keyword" label="关键词" width="180" />
              <el-table-column class="column" prop="count" label="总搜索量" />
              <el-table-column class="column" prop="users" label="搜索用户数" />
            </el-table>
            <el-pagination
              layout="prev, pager, next"
              :total="100"
              :page-size="4"
              background
              @current-change="onPageChange"
            />
          </div>
        </div>
      </el-card>
    </div>
    <div class="view">
      <el-card shadow="hover">
        <template v-slot:header>
          <div class="title-wrapper">
            <div class="title">分类销售排行</div>
            <div class="radio-wrapper">
              <el-radio-group v-model="radioSelect" size="small">
                <el-radio-button label="品类" />
                <el-radio-button label="商品" />
              </el-radio-group>
            </div>
          </div>
        </template>
        <div class="chart-wrapper">
          <v-chart :option="categoryOption" />
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchUserOption: {
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          show: false
        },
        series: [
          {
            type: 'line',
            data: [123, 652, 239, 353, 345, 255, 234, 345],
            areaStyle: {
              color: 'rgba(95,187,255,.5)'
            },
            lineStyle: {
              color: 'rgba(95,187,255)'
            },
            itemStyle: {
              opacity: 0
            },
            smooth: true
          }
        ],
        grid: {
          top: 0,
          left: 0,
          bottom: 0,
          right: 0
        }
      },
      searchNumberOption: {
        xAxis: {
          type: 'category',
          boundaryGap: false
        },
        yAxis: {
          show: false
        },
        series: [
          {
            type: 'line',
            data: [663, 252, 789, 183, 655, 155, 984, 645, 563],
            areaStyle: {
              color: 'rgba(95,187,255,.5)'
            },
            lineStyle: {
              color: 'rgba(95,187,255)'
            },
            itemStyle: {
              opacity: 0
            },
            smooth: true
          }
        ],
        grid: {
          top: 0,
          left: 0,
          bottom: 0,
          right: 0
        }
      },
      tableData: [
        {
          id: 1,
          rank: 1,
          keyword: '北京',
          count: 100,
          users: 90,
          range: '90%'
        },
        {
          id: 2,
          rank: 2,
          keyword: '北京',
          count: 100,
          users: 90,
          range: '90%'
        },
        {
          id: 3,
          rank: 3,
          keyword: '北京',
          count: 100,
          users: 90,
          range: '90%'
        },
        {
          id: 4,
          rank: 4,
          keyword: '北京',
          count: 100,
          users: 90,
          range: '90%'
        }
      ],
      radioSelect: '品类',
      categoryOption: {}
    }
  },
  methods: {
    onPageChange(page) {},
    renderPieChart() {
      const mockData = [
        {
          legendName: '粉面店',
          value: 66,
          percent: '18.56%',
          itemStyle: {
            color: '#e7e702'
          },
          name: '粉面店 | 18.56%'
        },
        {
          legendName: '快餐店',
          value: 97,
          percent: '35.36%',
          itemStyle: {
            color: '#8b7fec'
          },
          name: '快餐店 | 35.36%'
        },
        {
          legendName: '汉堡披萨',
          value: 92,
          percent: '53.76%',
          itemStyle: {
            color: '#5085f2'
          },
          name: '汉堡披萨 | 53.76%'
        }
      ]
      this.categoryOption = {
        title: [
          {
            text: '品类分布',
            textStyle: {
              fontSize: 14,
              color: '#666'
            },
            top: 20,
            left: 20
          },
          {
            text: '累计订单量',
            subtext: '320',
            x: '34%',
            y: '46%',
            textStyle: {
              fontSize: 14,
              color: '#999'
            },
            subtextStyle: {
              fontSize: 28,
              color: '#333'
            },
            textAlign: 'center'
          }
        ],
        series: [
          {
            name: '品类分布',
            type: 'pie',
            data: mockData,
            label: {
              normal: {
                show: true,
                formatter: function (params) {
                  return `${params.data.legendName} | ${params.data.percent}`
                }
              }
            },
            center: ['35%', '50%'],
            radius: ['45%', '60%'],
            labelLine: {
              normal: {
                length: 5,
                length2: 3,
                smooth: true
              }
            },
            clockwise: false,
            itemStyle: {
              borderWidth: 4,
              borderColor: '#fff'
            }
          }
        ],
        legend: {
          type: 'scroll',
          orient: 'vertical',
          height: 249,
          top: 'middle',
          left: '70%',
          textStyle: {
            color: '#8c8c8c'
          }
        },
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            const str =
              params.seriesName +
              '<br />' +
              params.marker +
              params.data.legendName +
              '<br />' +
              '数量：' +
              params.data.value +
              '<br />' +
              '占比：' +
              params.data.percent +
              '%'
            return str
          }
        }
      }
    }
  },
  mounted() {
    this.renderPieChart()
  }
}
</script>

<style lang="scss">
.bottom-view {
  display: flex;
  margin-top: 20px;
  .view {
    flex: 1;
    width: 50%;
    box-sizing: border-box;
    &:first-child {
      padding: 0 10px 0 0;
    }
    &:last-child {
      padding: 0 0 0 10px;
    }
    .title {
      padding-left: 20px;
      height: 60px;
      line-height: 60px;
      font-size: 14px;
      border-bottom: 1px solid #eee;
    }
    .title-wrapper {
      display: flex;
      align-items: center;
      padding: 0 0 0 20px;
      height: 60px;
      font-size: 14px;
      font-weight: 500;
      border-bottom: 1px solid #eee;
      box-sizing: border-box;
      .radio-wrapper {
        flex: 1;
        display: flex;
        justify-content: flex-end;
        padding-right: 20px;
      }
    }
    .chart-wrapper {
      display: flex;
      flex-direction: column;
      height: 425px;
      .chart-inner {
        display: flex;
        margin-top: 20px;
        padding: 0 10px;
        .chart {
          flex: 1;
          padding: 0 10px;
          .chart-title {
            color: #999;
            font-size: 14px;
          }
          .chart-data {
            color: #333;
            font-size: 22px;
            font-weight: 500;
            letter-spacing: 2px;
          }
          .echarts {
            height: 50px;
          }
        }
      }
      .table-wrapper {
        flex: 1;
        margin-top: 20px;
        padding: 0 20px 20px;
        .el-pagination {
          display: flex;
          justify-content: flex-end;
          margin-top: 15px;
        }
      }
    }
  }
}
</style>
